<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\AgricGood;
use App\Admin\Repositories\AgricGoodsCategory;
use App\Models\AgricGoods as AgricGoodsModel;
use App\Models\AgricGoodsSeller;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Controllers\AdminController;
use Dcat\Admin\Support\Helper;

class AgricGoodController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new AgricGood(), function (Grid $grid) {
            $grid->model()->orderBy('is_top', 'desc')->orderBy('sort', 'asc');
            $grid->model()->with(['seller_info']);

            $grid->column('id')->sortable();
            $grid->column('goods_name');
            $grid->column('goods_img')->image('', 60, 60);
            $grid->column('goods_price');
            $grid->column('acticity_price');

            $grid->column('is_show')->switch();
            $grid->column('is_top')->switch()->sortable();
            $grid->column('category_id')->display(function ($category_id) {
                if ($category_id) {
                    $ategory = \App\Models\AgricGoodsCategory::find($category_id);
                    return $ategory ? $ategory->category_name : '';
                } else {
                    return '';
                }
            });
            $grid->column('sort')->sortable();

            $grid->quickSearch(['goods_name', 'goods_place'])->placeholder('搜索...');
            $grid->selector(function (Grid\Tools\Selector $selector) {
                $category = AgricGoodsCategory::getList();
                $selector->selectOne('category_id', '分类筛选', $category);
                $selector->selectOne('is_show', '上架状态', [0 => '未上架', 1 => '已上架']);
                $selector->selectOne('is_top', '置顶状态', [0 => '未置顶', 1 => '已置顶']);
            });
//            $grid->filter(function (Grid\Filter $filter) {
//                $filter->equal('id');
//
//            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, AgricGoodsModel::with(['seller_info', 'goods_descriptions']), function (Show $show) {
            $show->field('id');
            $show->field('goods_name');
            $show->field('goods_img')->image();
//            $show->field('slider_img');
//            $show->field('goods_label');

            $show->field('seller_info.seller_name');
            $show->field('seller_info.seller_num');
            $show->field('seller_info.head_portrait')->image();
            $show->field('seller_info.wx');
            $show->field('goods_price');
            $show->field('acticity_price');
            $show->field('goods_unit');
            $show->field('goods_place');
            $show->field('is_show')->as(function ($is_show) {
                return $is_show ? '已上架' : '已下架';
            });
            $show->field('is_top')->as(function ($is_top) {
                return $is_top ? '已置顶' : '未置顶';
            });
            $show->field('sort');
            $show->field('interested_num');
            $show->field('sales_num');
            $show->field('category_id')->as(function ($category_id) {
                if ($category_id) {
                    $ategory = \App\Models\AgricGoodsCategory::find($category_id);
                    return $ategory ? $ategory->category_name : '';
                } else {
                    return '';
                }
            });
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(AgricGoodsModel::with(['seller_info', 'goods_descriptions']), function (Form $form) {

            $form->display('id');
            $category = \App\Admin\Repositories\AgricGoodsCategory::getList();
            $form->select('category_id', '商品分类')->options($category)->required()->default(0, true);
            $form->text('goods_name')->required();
            $form->image('goods_img')->move('goods/'.date('Y-m-d'))->autoUpload();
            $form->multipleImage('slider_img')->move('goods/'.date('Y-m-d'))->saving(function ($paths) {
                $paths = Helper::array($paths);
                return json_encode($paths, true); // 最终转化为json保存到数据库
            })->customFormat(function ($paths) {
                return Helper::array($paths); // 转为数组
            })->autoUpload();
            $form->editor('goods_descriptions.goods_description', '介绍');
            $form->table('goods_label', function (NestedForm $table) {
                $table->text('key');
                $table->text('value');
            });

            $seller = AgricGood::getSellerList();
            $form->select('seller_id', '选择卖家')->options($seller)->default(0, true);
            $form->currency('goods_price')->symbol('￥');
            $form->currency('acticity_price')->symbol('￥');
            $form->text('goods_unit')->placeholder('租赁商品必填。例如：天、周、月、年');
            $form->text('goods_place');
            $form->radio('is_show', '状态')->options([1 => '上架', 0 => '下架'])->default(1);
            $form->radio('is_top', '置顶')->options([1 => '是', 0 => '否'])->default(0);
            $form->number('sort')->value(1);
            $form->number('sales_num', '销量');

            $form->display('created_at');
            $form->display('updated_at');

            $form->submitted(function ($form) {
//                dd($form->goods_label);
//                dd($form->updates());
//                dd($form->model()->toArray());
//                $form->deleteInput('seller_type');

                // 商品名验证
                $query = AgricGoodsModel::query();
                $query->where('goods_name', $form->goods_name);
                if ($form->model()->id) {
                    $query->where('id', '!=', $form->model()->id);
                }
                $checkName = $query->first();
                if ($checkName) {
                    $form->responseValidationMessages('goods_name', '商品名称已存在');
                }
            });

//            $form->saved(function (Form $form, $result) {
//                $f_data = $form->updates();
//
//                $edit_seller = AgricGoodsSeller::where('seller_name', $f_data['seller_info']['seller_name'])
//                    ->where('seller_num', $f_data['seller_info']['seller_num'])
//                    ->where('wx', $f_data['seller_info']['wx'])
//                    ->where('head_portrait', $f_data['seller_info']['head_portrait'])
//                    ->first();
//
//                AgricGoods::where('id', $result)->update(['seller_id' => $edit_seller->id]);
//            });


        });
    }
}
